package cn.stylefeng.guns.modular.manager.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.manager.entity.FlowCoupon;
import cn.stylefeng.guns.modular.manager.mapper.FlowCouponMapper;
import cn.stylefeng.guns.modular.manager.model.params.FlowCouponParam;
import cn.stylefeng.guns.modular.manager.model.result.FlowCouponResult;
import  cn.stylefeng.guns.modular.manager.service.FlowCouponService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 优惠卷 服务实现类
 * </p>
 *
 * @author yumker
 * @since 2021-01-07
 */
@Service
public class FlowCouponServiceImpl extends ServiceImpl<FlowCouponMapper, FlowCoupon> implements FlowCouponService {

    @Override
    public void add(FlowCouponParam param){
        FlowCoupon entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(FlowCouponParam param){
        this.removeById(getKey(param));
    }

    @Override
    public void update(FlowCouponParam param){
        FlowCoupon oldEntity = getOldEntity(param);
        FlowCoupon newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public FlowCouponResult findBySpec(FlowCouponParam param){
        return null;
    }

    @Override
    public List<FlowCouponResult> findListBySpec(FlowCouponParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(FlowCouponParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(FlowCouponParam param){
        return param.getCouponId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private FlowCoupon getOldEntity(FlowCouponParam param) {
        return this.getById(getKey(param));
    }

    private FlowCoupon getEntity(FlowCouponParam param) {
        FlowCoupon entity = new FlowCoupon();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
